package com.nus.servlets;

import com.nus.beans.Project;
import com.nus.beans.ProjectRemark;
import com.nus.beans.User;
import com.nus.beans.UserEvaluation;
import com.nus.database.ProjectsDBManager;
import com.nus.database.UserInfoDBManager;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.Connection;

/**
 * Created by IntelliJ IDEA.
 * User: dell
 * Date: 11/3/12
 * Time: 1:38 PM
 * To change this template use File | Settings | File Templates.
 */
public class AddUserEvaluationsServlet extends AbstractServlet {
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {

        String evaluation = request.getParameter("evaluation");
        User user = (User) request.getSession().getAttribute("targetUser");

        User currentUser = (User) request.getSession().getAttribute("user");

        UserEvaluation userEvaluation = new UserEvaluation();
        userEvaluation.setEnteredBy(currentUser);
        userEvaluation.setEvaluation(evaluation);
        userEvaluation.setUserId(user.getUserId());

        Connection connection = getUserDatabaseConnection(request);
        UserInfoDBManager userInfoDBManager = new UserInfoDBManager(connection);

        userInfoDBManager.addEvaluation(userEvaluation);
        userInfoDBManager.close();

        request.setAttribute("targetUserId", user.getUserId());
        RequestDispatcher dispatcher = request.getRequestDispatcher("viewUserEvaluations.do");
        dispatcher.forward(request, response);
    }
}
